package com.sunday.common.core.utils;

/**
 * 62进制转换
 *
 * @author zsy
 * @since 2022/12/29
 */
public class CompressEncoding62Utils {

    private static String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    /**
     * 把10进制的数字转换成62进制
     *
     * @param number
     * @return {@link String}
     */
    public static String compressNumber(long number) {
        String ret = "";
        while (number > 0) {
            ret = characters.charAt((int) (number % 62)) + ret;
            number /= 62;
        }
        return ret;
    }

    /**
     * 把62进制的字符串转换成10进制
     *
     * @param compress
     * @return {@link long}
     */
    public static long unCompressNumber(String compress) {
        long ret = 0;
        compress = new StringBuffer(compress).reverse().toString();
        long count = 1;
        for (char character : compress.toCharArray()) {
            ret += characters.indexOf(character) * count;
            count *= 62;
        }
        return ret;
    }

}
